package com.witdata.controller.subject;

import com.witdata.common.domain.Result;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.QuestionDto;
import com.witdata.common.domain.dto.SqlCheckDto;
import com.witdata.common.domain.dto.SubjectQuestionDto;
import com.witdata.common.service.subject.QuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/question")
@RequiredArgsConstructor
public class QuestionController {
    private final QuestionService questionService;

    @GetMapping("/list")
    public Result<List<QuestionDto>> getQuestionList (
            @RequestParam("subjectId") Long subjectId
    ) {
        List<QuestionDto> questionDtoList = questionService.getQuestionListBySubjectId(subjectId);
        return Result.ok(Status.SUCCESS, questionDtoList);
    }

    @GetMapping("/list/type")
    public Result<List<QuestionDto>> getQuestionListByType (
            @RequestParam("subjectId") Long subjectId,
            @RequestParam(value = "type", defaultValue = "Choice") String type
    ) {
        List<QuestionDto> questionDtoList = questionService.getQuestionListBySubjectId(subjectId, type);
        return Result.ok(Status.SUCCESS, questionDtoList);
    }

    @GetMapping("/list/type/all")
    public Result<List<QuestionDto>> getQuestionList(@RequestParam(value = "type", defaultValue = "Choice") String type) {
        List<QuestionDto> questionDtoList = questionService.getQuestionListByType(type);
        return Result.ok(Status.SUCCESS, questionDtoList);
    }

    @PostMapping("/create")
    public Result<String> createQuestion(@RequestBody SubjectQuestionDto subjectQuestionDto) {
        questionService.createQuestion(subjectQuestionDto);
        return Result.ok(Status.CREATESUCCESS);
    }

    @DeleteMapping("/delete")
    public Result<String> deleteQuestion(@RequestParam("questionId") Long questionId) {
        questionService.deleteQuestionById(questionId);
        return Result.ok(Status.DELETESUCCESS);
    }

    @GetMapping("/latest")
    public Result<List<QuestionDto>> getLatestQuestion(
            @RequestParam("subjectId") Long subjectId,
            @RequestParam(value = "type", required = false) String type
    ) {
        List<QuestionDto> questionDtoList;
        if (StringUtils.hasText(type)) {
            questionDtoList = questionService.getLatestQuestionListBySubjectId(subjectId, type);
        } else {
            questionDtoList = questionService.getLatestQuestionListBySubjectId(subjectId);
        }
        return Result.ok(Status.SUCCESS, questionDtoList);
    }

    @GetMapping("/info")
    public Result<QuestionDto> getQuestionInfo(@RequestParam("id") Long id) {
        QuestionDto questionInfo = questionService.getQuestionInfo(id);
        return Result.ok(Status.SUCCESS, questionInfo);
    }

    @GetMapping("/submit")
    public Result<Boolean> validateAnswer(
            @RequestParam("questionId") Long questionId,
            @RequestParam("userAnswer") String[] userAnswer
    ){
        boolean b = questionService.validateAnswer(questionId, userAnswer);
        return Result.ok(Status.SUCCESS, b);
    }

    @PostMapping("/sql/check")
    public Result<String> checkSqlContent(@RequestBody SqlCheckDto sqlCheckDto) {
        questionService.checkSqlContent(sqlCheckDto);
        return Result.ok(Status.SUCCESS);
    }
}
